#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script"
    exit 1
fi

clear
. lnmp.conf
. include/main.sh
Check_Stack
Check_DB
php_version=""
Get_PHP_Ext_Dir
echo ""
echo "Current PHP Version: ${Cur_PHP_Version}"
echo -e "\e[0;32mYou have options for php version \e[0m:"
for phpfpmFile in $( find /usr/local/ -name  php-[57].* -type d )
do
    php_ver=$(echo $phpfpmFile | awk -F "-" '{print $3}')
    echo -e "   \e[0;31m${php_ver}\e[0m"
done
echo -e "   \e[0;31mexit\e[0m"
read -p "Enter your php version : " PHP_Ver

if echo ${PHP_Ver} | grep -Eqi "^[57]" ; then
   echo ${PHP_Ver}
 else
   exit
fi

if [[ -d /usr/local/php-${PHP_Ver} ]] ; then
    echo "php ${PHP_Ver} is found!"
    /etc/init.d/php-fpm stop
    rm -f /tmp/php-cgi.sock
    rm -f /etc/init.d/php-fpm
    cp /usr/local/php-${PHP_Ver}/php-fpm-${PHP_Ver} /etc/init.d/php-fpm
    chmod 755 /etc/init.d/php-fpm
    rm -f /usr/local/php
    ln -sf /usr/local/php-${PHP_Ver} /usr/local/php
    /etc/init.d/php-fpm start
 else
    echo "php ${PHP_Ver} is not found! Please run ./upgrade.sh"
fi

